package org.particlethink.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.particlethink.auth.DefaultUser;
import org.particlethink.context.Context;
import org.particlethink.context.DefaultContext;
import org.particlethink.mapper.CategoryMapper;
import org.particlethink.mapper.ProjectMapper;
import org.particlethink.model.Category;
import org.particlethink.model.Project;
import org.particlethink.model.User;
import org.particlethink.response.BaseResponse;
import org.particlethink.response.CategoryResponse;
import org.particlethink.response.MaterialKindResponse;
import org.particlethink.response.ProjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "分类")
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    public CategoryMapper categoryMapper;

//    @Operation(summary = "计划")
//    @GetMapping("/list")
//    public List<CategoryResponse> list() {
////        List<Project> projects = projectMapper.list(projectName, manageLvName);
////        return projects.stream().map(project -> new ProjectResponse(project)).collect(Collectors.toList());
////
////        土建、安装(水、电)、装饰、临建、安全生产费、文明施工费、周转材料、购置资产、冬施费用
//
//        List<Category> categories = categoryMapper.all();
//        return categories.stream().map(CategoryResponse::new).collect(Collectors.toList());
//    }


    @Operation(summary = "物料大类")
    @GetMapping("/large")
    public BaseResponse<List<MaterialKindResponse>> P() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        DefaultUser defaultUser = (DefaultUser) authentication.getPrincipal();
        User user = defaultUser.getUser();

        Context ctx = DefaultContext.Builder.background().withValue("token", ((DefaultUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getToken());
        List<Category> categories = categoryMapper.selectParents(user.getToken());
        List<MaterialKindResponse> collect = categories.stream()
                .map(category -> {
                    MaterialKindResponse response = new MaterialKindResponse();
                    response.setId(category.getId());
                    response.setName(category.getName());
                    return response;
                })
                .collect(Collectors.toList());
        return BaseResponse.success(collect);

    }

//    @Operation(summary = "格努")
//    @GetMapping("/list_kind")
//    public List<MaterialKindResponse> queryMaterialKind(@RequestParam @Parameter(description = "物料分类的父级ID，如果传0或null则返回物料大类") Integer parentId) {
//        List<Category> categories = categoryMapper.selectByParentId(parentId);
//        return categories.stream()
//                .map(category -> {
//                    MaterialKindResponse response = new MaterialKindResponse();
//                    response.setId(category.getId());
//                    response.setName(category.getName());
//                    return response;
//                })
//                .collect(Collectors.toList());
//    }
}
